﻿// --------------------------------
// <copyright file="ImageItem.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>27. 11. 2011 16:31:21</date>

// <summary>
//
// </summary>
// --------------------------------
#if WINDOWS

using System.ComponentModel;
using FITClub.Episode1.Levels;
using System.Drawing.Design;

#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace FITClub.TextureMenu
{
#if WINDOWS

    [System.Serializable]
#endif
    /// <summary>
    /// Inactive image menu item.
    /// </summary>
    public class ImageItem : ADisplayable
    {
        [XmlElement]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string TexturePath { get; set; }

        [XmlElement]
        public byte? Alpha { get; set; }

        [XmlAttribute]
        public bool FitToSize { get; set; }

        [XmlIgnore]
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; ComputeTextureBounds(); }
        }

#if WINDOWS
        [System.NonSerialized]
#endif
        private Texture2D texture;

        private Rectangle textureBounds;

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            if (this.ContentLoaded == false)
            {
                try /// tring in order to not to keep unused or invisible textures
                {
                    if (this.TexturePath != null && this.TexturePath.Trim() != string.Empty)
                    {
                        this.Texture = loader.Load<Texture2D>(this.TexturePath);                        
                    }
                    this.ContentLoaded = true;
                }
                catch (Exception exc)
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine(exc);
#endif
                }
            }
        }

        public override void Dispose()
        {
            if (this.ContentLoaded == false)
            {
                if (this.texture != null) this.texture.Dispose();
                this.ContentLoaded = false;
            }
        }

        protected void ComputeTextureBounds()
        {
            if (this.texture != null)
            {
                if (this.FitToSize)
                {
                    double scaleW = (double)this.Bounds.Width / (double)this.texture.Width;
                    double scaleH = (double)this.Bounds.Height / (double)this.texture.Height;
                    double scale = Math.Min(scaleH, scaleW);
                    int width = (int)((double)this.texture.Width * scale);
                    int height = (int)((double)this.texture.Height * scale);
                    int dx = (this.Bounds.Width - width) / 2;
                    int dy = (this.Bounds.Height - height) / 2;
                    this.textureBounds = new Rectangle(this.Bounds.X + dx, this.Bounds.Y + dy, width, height);
                }
                else
                {
                    this.textureBounds = this.Bounds;
                }
            }
        }

        public override void Move(Point delta)
        {
            base.Move(delta);
            ComputeTextureBounds();
        }

        

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.Color tint)
        {
            if (Alpha != null)
            {
                tint.A = this.Alpha.Value;
            }
            if (this.texture != null) spriteBatch.Draw(this.texture, this.textureBounds, tint);
        }
    }
}